using MicroMsg.Common.Event;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using System;
using System.Windows;
namespace MicroMsg.Network
{
	public class ServerBulletin
	{
		public class BulletinInfo
		{
			public int type;
			public int validTime;
			public string strHost = "";
		}
		private const string TAG = "SeverBulletin";
		private static ServerBulletin.BulletinInfo mBulletinInfo = new ServerBulletin.BulletinInfo();
		private static TimerObject mTimerObject = null;
		private static bool isTimeoutDate(uint time)
		{
			DateTime dateTime = DateTime.get_Now().ToUniversalTime();
			DateTime dateTime2 = new DateTime(1970, 1, 1);
			double totalSeconds = dateTime.Subtract(dateTime2).get_TotalSeconds();
			return (uint)totalSeconds > time;
		}
		public static bool isFaultMode()
		{
			if (ServerBulletin.mBulletinInfo.type != 1)
			{
				return false;
			}
			if (ServerBulletin.isTimeoutDate((uint)ServerBulletin.mBulletinInfo.validTime))
			{
				ServerBulletin.mBulletinInfo.type = 0;
				return false;
			}
			return true;
		}
		public static string getBulletinUrl(string username, string lang)
		{
			return string.Format("http://w.mail.qq.com/cgi-bin/report_mm?failuretype=1&devicetype={0}&clientversion={1}&os={2}&username={3}&iport={4}&t=weixin_bulletin&f=xhtml&lang={5}", new object[]
			{
				5,
				ConstantsProtocol.CLIENT_VERSION,
				Environment.get_OSVersion().get_Version(),
				username,
				ServerBulletin.mBulletinInfo.strHost,
				lang
			});
		}
		public static void onRecvBulletin(byte[] data)
		{
			if (data != null && data.Length >= 8)
			{
				bool flag = ServerBulletin.isFaultMode();
				int num = 0;
				ServerBulletin.mBulletinInfo.type = Util.readInt(data, ref num);
				ServerBulletin.mBulletinInfo.validTime = Util.readInt(data, ref num);
				ServerBulletin.mBulletinInfo.strHost = HostService.mLongConnHosts.getCurrentHostString();
				Log.smethod_4("SeverBulletin", string.Concat(new object[]
				{
					"received bulletin：type =",
					ServerBulletin.mBulletinInfo.type,
					", validtime=",
					ServerBulletin.mBulletinInfo.validTime
				}));
				bool flag2;
				if ((flag2 = ServerBulletin.isFaultMode()) != flag)
				{
					EventCenter.postEvent(EventConst.ON_NET_BULLETIN, flag2, null);
					if (flag2)
					{
						ServerBulletin.startCheckTimer();
					}
				}
				return;
			}
			Log.smethod_1("SeverBulletin", "received invalid bulletin data, ignored.");
		}
		public static void onRecvBulletin(string headcontent)
		{
			if (string.IsNullOrEmpty(headcontent))
			{
				return;
			}
			string[] array = headcontent.Split(new char[]
			{
				','
			});
			if (array != null && array.Length >= 2)
			{
				bool flag = ServerBulletin.isFaultMode();
				ServerBulletin.mBulletinInfo.type = Util.stringToInt(array[0]);
				ServerBulletin.mBulletinInfo.validTime = Util.stringToInt(array[1]);
				ServerBulletin.mBulletinInfo.strHost = HostService.mShortConnHosts.getCurrentHostString();
				Log.smethod_4("SeverBulletin", string.Concat(new object[]
				{
					"received bulletin in http headers：type =",
					ServerBulletin.mBulletinInfo.type,
					", validtime=",
					ServerBulletin.mBulletinInfo.validTime
				}));
				bool flag2;
				if ((flag2 = ServerBulletin.isFaultMode()) != flag)
				{
					EventCenter.postEvent(EventConst.ON_NET_BULLETIN, flag2, null);
					if (flag2)
					{
						ServerBulletin.startCheckTimer();
					}
				}
				return;
			}
		}
		private static void startCheckTimer()
		{
			if (ServerBulletin.mTimerObject == null)
			{
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					ServerBulletin.mTimerObject = TimerService.addTimer(20, new EventHandler(ServerBulletin.onCheckFaultModeTimeout));
					ServerBulletin.mTimerObject.start();
				});
			}
		}
		private static void closeCheckTimer()
		{
			if (ServerBulletin.mTimerObject != null)
			{
				ServerBulletin.mTimerObject.stop();
				ServerBulletin.mTimerObject = null;
			}
		}
		public static void onCheckFaultModeTimeout(object sender, EventArgs e)
		{
			if (ServerBulletin.mBulletinInfo.type != 1)
			{
				ServerBulletin.closeCheckTimer();
				return;
			}
			if (!ServerBulletin.isTimeoutDate((uint)ServerBulletin.mBulletinInfo.validTime))
			{
				return;
			}
			ServerBulletin.mBulletinInfo.type = 0;
			EventCenter.postEvent(EventConst.ON_NET_BULLETIN, false, null);
			ServerBulletin.closeCheckTimer();
		}
	}
}
